import { http } from "@/utils/http";

export type UserResult = {
  code: number;
  msg: string;
  data?: any;
  accessToken?: string;
  refreshToken?: string;
  success?: boolean;
  token: string;
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};
export type UserInfo = {
  id: number;
  username: string;
  nickname: string;
  email: string;
  sex: number;
  avatar: string;
  real_name: string;
  birthday: string;
  phone: string;
  roles: string;
  permissions: string[];
  headimgurl: string;
  introduction: string;
  address: string;
  created_at: string;
};

export type UserInfoResult = {
  code: number;
  message: string;
  data?: UserInfo;
};

export type ResultTable = {
  success: boolean;
  data?: {
    /** 列表数据 */
    list: Array<any>;
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
};

/** 登录 */
export const getLogin = (data?: object) => {
  // 后端真实接口
  return http.request<UserResult>("post", "/api/login", { data });
};
/** 注册 */
export const goRegister = (data?: object) => {
  return http.request<UserResult>("post", "/api/register-admin", { data });
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/api/refresh-token", {
    data
  });
};

/** 获取用户信息 */
export const getUserInfo = () => {
  return http.request<UserInfoResult>("get", "/api/user/info");
};
/** 更新用户信息 */
export const updateUserInfo = (data?: object) => {
  return http.request<UserInfoResult>("put", "/api/updateInfo", { data });
};

/** 账户设置-个人安全日志 */
export const getMineLogs = (data?: object) => {
  return http.request<ResultTable>("get", "/api/mine-logs", { data });
};
/** 账户设置-退出登录 */
export const logout = () => {
  return http.request<UserResult>("post", "/api/logout");
};

/** 重置用户密码 */
export const resetUserPassword = (userId: number, data: { oldPassword?: string; newPassword?: string }) => {
  return http.request<UserInfoResult>("post", `/api/users/${userId}/reset-password`, { data });
};
